<?php
class Default_Model_Users extends Zend_Db_Table{
	protected $_name = 'users';
	protected $_primary = 'id';

	public function countItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$ssFilter = $arrParam['ssFilter'];
		
		$select = $db->select()
					->from('users AS u',array('COUNT(u.id) AS totalItem'));
		
		if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('u.user_name LIKE ?',$keywords,"STRING");
		}
		if($options['task']=="developer-statistical"){
			$select->join('developer_files AS d','d.developer_id=u.id','COUNT(d.id) AS number_app')
			->group('d.developer_id');
		}
		
		if($ssFilter['group_id']>0){
			$select->where('u.group_id = ?',$ssFilter['group_id'],"INTEGER");
		}
		//echo $select;
		$result = $db->fetchOne($select);
		return $result;
		
	}
	
	public function sortItem($arrParam = null, $options = null){
			
		$cid = $arrParam['cid'];
		$order = $arrParam['order'];
		if(count($cid)>0){
			foreach ($cid as $key => $val) {
				$where = ' id = ' . $val;				
				$data = array('order'=>$order[$val]);
				$this->update($data,$where);
			}
		}
	}
	
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
		
		if($options['task'] == 'admin-list'){
			$select = $db->select()
						 ->from('users AS u',array('id','user_name','status','email','register_date'))
						 ->joinLeft('user_group AS g','g.id = u.group_id','group_name');
			
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			
			}
									 
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
			
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('u.user_name LIKE ?',$keywords,"STRING");
			}
			
			if($ssFilter['group_id']>0){
				$select->where('u.group_id = ?',$ssFilter['group_id'],"INTEGER");
			}
			$result  = $db->fetchAll($select);
		}
		
		if($options['task'] == 'developer-statistical'){
			$select = $db->select()
			->from('users AS u')
			->joinLeft('user_group AS g','g.id = u.group_id','group_name')
			//->joinLeft('developer_files AS d','d.developer_id=u.id',array('COUNT(d.id) AS number_app','d.developer_id'))
			//->group('d.developer_id')
			->order('u.score DESC')
			->where('u.group_id=?',3) ;
// 			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
// 				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
					
// 			}
		
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
		
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('u.user_name LIKE ?',$keywords,"STRING");
			}
		
			if($ssFilter['group_id']>0){
				$select->where('u.group_id = ?',$ssFilter['group_id'],"INTEGER");
			}
			$result  = $db->fetchAll($select);
		}
		
		return $result;
	
	}
	
	public function saveItem($arrParam = null, $options = null){
		
		if($options['task'] == 'admin-add'){
			$row =  $this->fetchNew();
			$encode  = new Zendvn_Encode();
			$row->user_name 	= $arrParam['user_name'];
			$row->user_avatar 	= $arrParam['user_avatar'];
			$row->password 		= $encode->password($arrParam['password']);
			$row->email 		= $arrParam['email'];
			$row->group_id 		= $arrParam['group_id'];
			$row->first_name 	= $arrParam['first_name'];
			$row->last_name 	= $arrParam['last_name'];
			$row->birthday 		= $arrParam['birthday'];
			$row->status 		= $arrParam['status'];
			$row->sign 			= $arrParam['sign'];
			$row->register_date	= date("Y-m-d");
			$row->register_ip	= $_SERVER['REMOTE_ADDR'];
						
			$row->save();
		}
		
		if($options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];
			
			$row =  $this->fetchRow($where);
			$encode  = new Zendvn_Encode();
			$row->user_name 	= $arrParam['user_name'];
			if(!empty($arrParam['user_avatar'])){
				$row ->user_avatar 	= $arrParam['user_avatar'];
			}
			
			if(!empty($arrParam['password'])){
				$row->password 		= $encode->password($arrParam['password']);
			}
			$row->email 		= $arrParam['email'];
			$row->group_id 		= $arrParam['group_id'];
			$row->first_name 	= $arrParam['first_name'];
			$row->last_name 	= $arrParam['last_name'];
			$row->birthday 		= $arrParam['birthday'];
			$row->status 		= $arrParam['status'];
			$row->sign 			= $arrParam['sign'];
			$row->register_date	= date("Y-m-d");
			$row->register_ip	= $_SERVER['REMOTE_ADDR'];
			
			$row->save();
		}
		
	}
	
	public function getItem($arrParam = null, $options = null){
		
		if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter,$config);
			$select = $db->select()
					     ->from('users as u')
					     ->joinLeft('user_group as g','u.group_id = g.id',array('group_name'))
					     ->where('u.id = ?', $arrParam['id'], "INTEGER" );
			
			$result = $db->fetchRow($select);
		}
		
		if($options['task'] == 'developer-statistical'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter,$config);
			$select = $db->select()
			->from('users as u')
			->joinLeft('user_group as g','u.group_id = g.id',array('group_name'))
			->join('developer_files AS d','d.developer_id=u.id','COUNT(d.id) AS number_app')
			->group('d.developer_id')
			->where('u.id = ?', $arrParam['id'], "INTEGER" );
				
			$result = $db->fetchRow($select);
		}
		
		//---- de lay ra ten file anh can delete
		if($options['task']=='delete'){
			$where = 'id='.$arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
			
		}
		return $result;
	}
	
	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-delete'){
			$where = ' id = ' . $arrParam['id'];
			//----- LAY TEN HINH ANH user_avatar
			$row = $this->getItem($arrParam,array('task'=>'delete')) ;
			$imgName = $row['user_avatar'];
			//------ XOA HINH ANH TREN SERVER
			if(!empty($imgName)){
				$upload_dir = FILES_PATH.'/users' ;
				$upload = new Zendvn_File_Upload() ;
				$upload->removeFile($upload_dir . '/orignal/' . $imgName) ;
				$upload->removeFile($upload_dir . '/img100x100/' . $imgName) ;
				$upload->removeFile($upload_dir . '/img450x450/' . $imgName) ;
			}
			$this->delete($where);
		}
		
		if($options['task'] == 'admin-multi-delete'){
			$cid = $arrParam['cid'];
			
			if(count($cid)>0){
// 				if($arrParam['type'] == 1){
// 					$status = 1;
// 				}else{
// 					$status = 0;
// 				}
				foreach($cid as $key=>$value){
					$arrParam['id'] = $value  ;
					//----- LAY TEN HINH ANH user_avatar
					$row = $this->getItem($arrParam,array('task'=>'delete')) ;
					$imgName = $row['user_avatar'];
					//------ XOA HINH ANH TREN SERVER
					if(!empty($imgName)){
						$upload_dir = FILES_PATH.'/users' ;
						$upload = new Zendvn_File_Upload() ;
						$upload->removeFile($upload_dir . '/orignal/' . $imgName) ;
						$upload->removeFile($upload_dir . '/img100x100/' . $imgName) ;
						$upload->removeFile($upload_dir . '/img450x450/' . $imgName) ;
					}
				}
				echo '<br>' . $ids = implode(',',$cid);
				echo '<br>' . $where = 'id IN (' . $ids . ')';
				$this->delete($where);
			}
		}
	}

	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		
		if(count($cid)>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			
			$ids = implode(',',$cid);
			$data = array('status'=>$status);
			$where = 'id IN (' . $ids . ')';
			$this->update($data,$where);
		}
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			
			$data = array('status'=>$status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data,$where);
		}
		
	}
	public function getTopScore() {
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		$select = $db->select()
		->from('users as u',array('id','score','user_name','user_avatar'))
		->order("u.score DESC")
		->limit(5)
		;
		$result  = $db->fetchAll($select);
	
		return $result;
	}
	
	public function listInfoUser($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
	
		$select = $db->select()
					->from('users AS u',array('id','user_name','user_avatar','status','email','register_date','score','phone'))
					->where('u.id=?',$arrParam['id']);
	
		$result  = $db->fetchAll($select);
		return $result;
	}
	public function friendList($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
					->from('friendlist AS f',array('name_friend','img_avatar'))
					->where('id_user=?',$arrParam['id']);
		$result = $db->fetchAll($select);
		return $result;
	}
	
}